{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 10,  20,  30,  40,  50,  60,  70,  80,  90, 100])"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "a=np.arange(10,110,10)\n",
    "a[3:9] # 索引从3到9的元素\n",
    "a[3:9:2] # 索引从3到9的元素，每隔2个取一个\n",
    "a\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([30, 50, 60])"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "index=[2,4,5]  # 索引列表\n",
    "a[index]  # 索引列表中的元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[20, 40, 60],\n",
       "       [30, 50, 70]])"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "index=np.array([[1,3,5],[2,4,6]]) #创建2维数组索引\n",
    "a[index] # 返回的是索引对应的二维数组元素"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "神奇索引用于二维数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 2, 4],\n",
       "       [5, 7, 9]])"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x=np.arange(10).reshape(2,-1) #2行，列数自动计算\n",
    "row=np.array([0,1,0])\n",
    "col=np.array([0,2,4])\n",
    "x[row,col] # 分别取出行0、1、0列0、2、4的值\n",
    "x[0,col] # 取出行0列0、2、4的值\n",
    "x[row,:2] #取出行0、1、0 前2列的值\n",
    "col2=[True,False,True,False,True]\n",
    "x[:,col2] #取出所有行，列0、2、4的值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ True,  True,  True,  True,  True],\n",
       "       [False, False, False, False, False]])"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x<5 # 输出数组中每一个元素是否小于5\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "比较结果和神奇索引的使用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.False_"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x[x>6] #取出x中大于6的元素\n",
    "x[x%2==0] #取出x中偶数的元素\n",
    "x[x%2!=0] #取出x中奇数的元素\n",
    "\n",
    "np.count_nonzero(x>6) #统计x中大于6的元素的个数\n",
    "np.sum(x>6) #统计x中大于6的元素的和\n",
    "np.any(x>6) #判断x中是否有大于6的元素\n",
    "np.all(x>6) #判断x中是否全为大于6的元素"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "多条件的使用: 与&或|非~"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.int64(7)"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sum((x<2)|(x>4)) # 计算x中小于2或大于4的元素的个数"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "machineLearn",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
